﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Its_static
{
    public partial class Main : Form
    {
        
        
        public Main()
        {
            InitializeComponent();
            loadSettings();

        }
        private void loadSettings()
        {
            layoutBrowseText.Text = Properties.Settings.Default.layoutPath;
            postBrowseText.Text = Properties.Settings.Default.postPath;
            sitesBrowseText.Text = Properties.Settings.Default.sitePath;
            Properties.Settings.Default.poststring = Properties.Settings.Default.noOfPosts.ToString();
            siteLinkTextBox.Text = Properties.Settings.Default.siteLink;

        }
       
        private void browseLayout_Click(object sender, EventArgs e)
        {
            openLayoutFolder.ShowDialog();
            layoutBrowseText.Text = openLayoutFolder.SelectedPath;
           
        }

        private void browsePost_Click(object sender, EventArgs e)
        {
            openPostsFolder.ShowDialog();
            postBrowseText.Text = openPostsFolder.SelectedPath;
            
        }

        private void browseSite_Click(object sender, EventArgs e)
        {
            openSitesFolder.ShowDialog();
            sitesBrowseText.Text = openSitesFolder.SelectedPath;
            
        }

        private void GeneratorButton_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.layoutPath = layoutBrowseText.Text;
            Properties.Settings.Default.postPath = postBrowseText.Text;
            Properties.Settings.Default.sitePath = sitesBrowseText.Text;
            Properties.Settings.Default.Save();
            File generatefile = new File();
            if (sitesBrowseText.Text.Length == 0)
                errorMsg("No site folder path!");
            else if (postBrowseText.Text.Length == 0)
                errorMsg("No post folder path!");
            else if (layoutBrowseText.Text.Length == 0)
                errorMsg("No layout folder path!");
            else if (Properties.Settings.Default.noOfPosts <= 0)
                errorMsg("Post number has to be more than 1!");
            else if (Properties.Settings.Default.siteLink.Length == 0)
                errorMsg("Enter the site link in settings!");

            else
            {
                generatefile.UpdateAll(postBrowseText.Text, layoutBrowseText.Text, sitesBrowseText.Text);
                MessageBox.Show("Done!");
            }
        }
        private void errorMsg(string error)
        {
            MessageBox.Show(error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            

        }

        private void saveSettingsButton_Click(object sender, EventArgs e)
        {
            try
            {
                Properties.Settings.Default.noOfPosts = int.Parse(noOfPostsTextBox.Text);
            }
            catch (System.FormatException i)
            {

                errorMsg("No. of posts isn't a number!");
            }
            Properties.Settings.Default.poststring = noOfPostsTextBox.Text;
            Properties.Settings.Default.siteLink = siteLinkTextBox.Text;
            Properties.Settings.Default.Save();
            MessageBox.Show("Done!");
        }

        
    }
}
